package com.taobao.home.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taobao.home.model.dto.GoodsPreviewDto;
import com.taobao.home.model.po.Goods;
import com.taobao.home.model.po.GoodsDetails;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 商品Mapper接口
 */
public interface GoodsMapper extends BaseMapper<Goods> {

    /**
     * 推荐页商品预览查询
     * SELECT * FROM table_name WHERE condition LIMIT start, length; 开始行号 和查询长度
     */
    @Select("SELECT goods_id, img_url, price, remark, title, _count FROM goods WHERE 1 =1 LIMIT #{pageNo}, #{pageSize}")
    List<GoodsPreviewDto> queryPreview(@Param("pageNo") Long pageNo, @Param("pageSize") Long pageSize);

    /**
     * 根据商品id查询商品详情
     */
    @Select("SELECT goods_id, video_url,img_list, price, remark, title, _count, evaluate_id, details_id,recommend_id,store_id FROM goods WHERE goods_id = #{goodsId}")
    GoodsDetails getGoodsDetail(Integer goodsId);

    /**
     * 根据id查询
     */
    @Select("SELECT * FROM goods WHERE goods_id = #{goodsId}")
    @Result(column = "_count", property = "_count")
    // xml文件有病，死活映射不上
    Goods getGoodsById(Integer goodsId);

    /**
     * 根据id查询可购买数量
     */
    @Select("SELECT goods_id, _count FROM goods WHERE goods_id = #{goodsId}")
    @Result(column = "_count", property = "_count")
    // xml文件有病，死活映射不上
    Goods getGoodsCountById(Integer goodsId);

    @Select("SELECT _count FROM goods WHERE goods_id = #{goodsId}")
    Integer test(Integer goodsId);

    /**
     * 根据title查询
     */
    @Select("SELECT goods_id,store_id, img_url, title, food_tag, price, _count, logistics "
            + "FROM goods "
            + "WHERE title LIKE CONCAT('%', #{title}, '%')")
    List<Goods> getGoodsPreviewByTitle(String title);

    /**
     * 根据storeId查询
     */
    @Select("SELECT * FROM goods WHERE store_id = #{storeId}")
    List<Goods> getGoodsByStoreId(@Param("storeId") int storeId);

    @Select("SELECT * FROM goods WHERE store_id = #{storeId} AND title LIKE CONCAT('%', #{title}, '%')")
    List<Goods> queryStoreIdAndTitle(@Param("storeId") int storeId, @Param("title") String  title);

    /**
     * 改库存
     */
    @Update("UPDATE goods SET _count = _count - #{num} WHERE goods_id = #{goodsId}")
    int updateGoodsCount(@Param("goodsId")int goodsId, @Param("num")int num);

    /**
     * 根据storeId删除
     */
    @Delete("DELETE FROM goods WHERE store_id = #{storeId}")
    int deleteGoodsByStoreId(int storeId);

}
